package com.kfm.shop.controller;

import com.alibaba.fastjson.JSON;
import com.kfm.shop.domain.Order;
import com.kfm.shop.domain.Product;
import com.kfm.shop.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private DiscoveryClient discoveryClient;

    @RequestMapping("/demo1")
    public String demo1() {
        return "demo1";
    }

    // 下单
    @RequestMapping("/prod/{pid}/{uid}")
    public Order order(@PathVariable("pid") Integer pid, @PathVariable("uid") Integer uid) {
        log.info("接收到{}用户的下单请求,接下来调用用户微服务查询此用户信息", uid);
        // 调用用户微服务，查询用户信息
        User user = restTemplate.getForObject("http://localhost:8070/user/find/" + uid, User.class);
        log.info("查询到{}用户的信息,内容是:{}", uid, JSON.toJSONString(user));
        log.info("接收到{}号商品的下单请求,接下来调用商品微服务查询此商品信息", pid);
        // 调用商品微服务,查询商品信息
        Product product = restTemplate.getForObject("http://localhost:8080/product/find/" + pid, Product.class);
        log.info("查询到{}号商品的信息,内容是:{}", pid, JSON.toJSONString(product));
        Order order = new Order();
        order.setId(1);
        order.setUid(uid);
        order.setUsername(user.getUsername());
        order.setPid(pid);
        order.setPname(product.getName());
        order.setPprice(product.getPrice());
        order.setNumber(1);
        // TODO 保存订单信息到 数据库
        log.info("创建订单成功,订单信息为{}", JSON.toJSONString(order));
        return order;
    }

    @RequestMapping("/prod2/{pid}/{uid}")
    public Order order2(@PathVariable("pid") Integer pid, @PathVariable("uid") Integer uid) {
        log.info("接收到{}用户的下单请求,接下来调用用户微服务查询此用户信息", uid);
        // 调用用户微服务，查询用户信息
        ServiceInstance userService = discoveryClient.getInstances("user").get(0);
        userService.getHost();
        int port = userService.getPort();
        String host = userService.getHost();
        String serviceId = userService.getServiceId();
        URI uri = userService.getUri();
        System.out.printf("host:%s,port:%d,serviceId:%s,uri:%s", host, port, serviceId, uri);
        User user = restTemplate.getForObject(uri + "/user/find/" + uid, User.class);
        log.info("查询到{}用户的信息,内容是:{}", uid, JSON.toJSONString(user));
        log.info("接收到{}号商品的下单请求,接下来调用商品微服务查询此商品信息", pid);
        // 调用商品微服务,查询商品信息
        ServiceInstance productService = discoveryClient.getInstances("product").get(0);
        Product product = restTemplate.getForObject(productService.getUri() + "/product/find/" + pid, Product.class);
        log.info("查询到{}号商品的信息,内容是:{}", pid, JSON.toJSONString(product));
        Order order = new Order();
        order.setId(1);
        order.setUid(uid);
        order.setUsername(user.getUsername());
        order.setPid(pid);
        order.setPname(product.getName());
        order.setPprice(product.getPrice());
        order.setNumber(1);
        // TODO 保存订单信息到 数据库
        log.info("创建订单成功,订单信息为{}", JSON.toJSONString(order));
        return order;
    }

    /*
        自定义负载均衡
     */
    @RequestMapping("/prod3/{pid}/{uid}")
    public Order order3(@PathVariable("pid") Integer pid, @PathVariable("uid") Integer uid) {
        // log.info("接收到{}用户的下单请求,接下来调用用户微服务查询此用户信息", uid);
        // // 调用用户微服务，查询用户信息
        // ServiceInstance userService = discoveryClient.getInstances("user").get(0);
        // User user = restTemplate.getForObject(userService.getUri() + "/user/find/" + uid, User.class);
        // log.info("查询到{}用户的信息,内容是:{}", uid, JSON.toJSONString(user));
        // log.info("接收到{}号商品的下单请求,接下来调用商品微服务查询此商品信息", pid);
        // 调用商品微服务,查询商品信息
        // List<ServiceInstance> instances = discoveryClient.getInstances("product");
        // ServiceInstance productService = instances.get(new Random().nextInt(instances.size()));
        // URI uri = productService.getUri();
        // System.out.printf("uri:%s", uri);
        // System.out.println();
        // 直接用服务名称替换 ip:port
        String url = "product-service"; // 直接使用 服务的名称 在 nacos 中获取服务的地址
        Product product = restTemplate.getForObject("http://product-service/product/find/" + uid, Product.class);
        log.info("查询到{}号商品的信息,内容是:{}", pid, JSON.toJSONString(product));
        Order order = new Order();
        order.setId(1);
        order.setUid(uid);
        order.setUsername("测试");
        order.setPid(pid);
        order.setPname(product.getName());
        order.setPprice(product.getPrice());
        order.setNumber(1);
        // TODO 保存订单信息到 数据库
        log.info("创建订单成功,订单信息为{}", JSON.toJSONString(order));
        return order;
    }
}
